"""
Case Type   : cm_ctl
Case Name   : 执行cm_ctl pause后,备机故障-kill -19 dn备节点
Create At   : 2023/04/10
Owner       : opentestcase012
Description :
    1.修改参数值enable_e2e为1,reload生效;
    2.执行cm_ctl pause;
    3.查看集群状态;
    4.备机故障-kill -19 dn进程;
    5.查看集群状态;
    6.恢复集群状态;
    7.查看集群状态;
    8.恢复参数值enable_e2e,reload生效;
Expect      :
    1.修改成功
    2.执行成功
    3.集群状态正常,有暂停标识
    4.执行成功
    5.集群状态异常不能自动恢复,kill备节点状态异常，其他节点状态正常
    6.恢复成功
    7.cm进行仲裁选主,集群状态恢复正常
    8.参数恢复成功
History     :
"""
import os
import time
import unittest
from yat.test import Node

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

primary_sh = CommonSH('PrimaryDbUser')


# @unittest.skipIf(2 != primary_sh.get_node_num(), '非1+2环境不执行')
class DefinedResource(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.common = Common()
        self.constant = Constant()
        self.sta1_user = Node('Standby1DbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')

        text = '----查看参数默认值; expect:成功----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl(
            'list', '--param --agent | grep enable_e2e_rto')
        self.assertIsNotNone(cm_ctl_res, '执行失败:' + text)
        self.default_value = cm_ctl_res.splitlines()[0].split('=')[1].strip()
        self.log.info(self.default_value)

    def test_cm(self):
        text = '----step1:修改参数值enable_e2e为1,reload生效; expect:成功----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl_set(
            'agent', 'enable_e2e_rto=1')
        self.assertTrue(cm_ctl_res, '执行失败:' + text)

        text = '----step2:执行cm_ctl pause; expect:暂停成功----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('pause')
        self.assertIn(self.constant.cm_pause_success_msg, cm_ctl_res,
                      '执行失败:' + text)

        text = '----step3:查看集群状态; expect:集群状态正常,有暂停标识----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('query', '-Civ')
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, cm_ctl_res,
                      '执行失败:' + text)
        self.assertIn('pausing         : Yes', cm_ctl_res, '执行失败:' + text)

        text = '----step4:备机故障-kill -19 dn进程; expect:执行成功----'
        self.log.info(text)
        kill_cmd = '''ps ux|grep "gaussdb -D"|grep -v grep|\
            tr -s ' '|cut -d ' ' -f 2|xargs kill -19;'''
        self.log.info(kill_cmd)
        process_dn = self.common.get_sh_result(self.sta1_user, kill_cmd)
        self.assertEqual('', process_dn, "执行失败:" + text)

        time.sleep(10)
        text = '----step5:查看集群状态; expect:集群状态异常不能自动恢复,kill备节点状态异常，其他节点状态正常----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('query', '-Civ')
        self.assertIn('S Standby Unknown', cm_ctl_res, '执行失败:' + text)
        self.assertEqual(2, cm_ctl_res.count('Normal'), "执行失败:" + text)

        text = '----step6:恢复集群状态; expect:恢复成功----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('resume')
        self.assertIn(self.constant.cm_resume_success_msg, cm_ctl_res,
                      '执行失败:' + text)

        time.sleep(30)
        text = '----step7:查看集群状态; expect:cm进行仲裁选主,集群状态恢复正常----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('query', '-Civ')
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, cm_ctl_res,
                      '执行失败:' + text)
        self.assertNotIn('pausing         : Yes', cm_ctl_res, '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text = '---恢复参数值enable_e2e为0,reload生效; expect:成功----'
        self.log.info(text)
        cm_ctl_res0 = self.primary_sh.exec_cm_ctl_set(
            'agent', f'enable_e2e_rto={self.default_value}')

        text = '----恢复集群状态; expect:执行成功----'
        self.log.info(text)
        cm_ctl_res1 = self.primary_sh.exec_cm_ctl('resume')
        cm_ctl_res2 = self.primary_sh.exec_cm_ctl('start')
        cm_ctl_res3 = self.primary_sh.exec_cm_ctl('switchover', '-a')
        time.sleep(10)
        cm_ctl_res4 = self.primary_sh.exec_cm_ctl('query', '-Civ')

        self.assertTrue(cm_ctl_res0, '执行失败:' + text)
        self.assertIn(self.constant.cm_resume_success_msg, cm_ctl_res1,
                      '执行失败:' + text)
        self.assertIn(self.constant.cm_start_success_msg, cm_ctl_res2,
                      '执行失败:' + text)
        self.assertIn(self.constant.cm_switchover_success_msg, cm_ctl_res3,
                      '执行失败:' + text)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, cm_ctl_res4,
                      "执行失败:" + text)
        self.log.info(f'----{os.path.basename(__file__)} finish----')
